<template>
	<view>
		<view class="box">
			<view class="list">
				<p> 头像： </p>
				<button open-type="chooseAvatar" class="avatar-wrapper" @chooseavatar="onChooseAvatar">
					<image class="headerimg" :src="avatarUrl"></image>
				</button>
			</view>

			<view class="list">
				<p> 昵称： </p>
				<input type="nickname" class="nikename" v-model="nickname" @blur="getnickname" placeholder="请输入昵称">
			</view>

			<view style="margin-top: 40rpx;">
				<button class="save" @click="save()">确定</button>
			</view>
			<view style="margin-top: 20rpx;">
				<button class="save logout" @click="confirmLogout()">退出</button>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname: '',
				avatarUrl: '',
				uploadInfo: {},
				userInfo: {},
				isUpdateAvatar: false,
			};
		},
		onLoad() {
			this.avatarUrl = uni.getStorageSync('avatar');
			this.userInfo = uni.getStorageSync('userInfo');

			this.nickname = this.userInfo.nickname || '';
		},
		onShow() {

		},
		onReady() {

		},
		mounted() {

		},
		methods: {
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail
				this.avatarUrl = avatarUrl
				this.avatar = avatarUrl
				this.isUpdateAvatar = true;
				this.upload();
				console.log(e);
			},
			getnickname(e) {
				const { value } = e.detail;
				this.nickname = value || '';
			},
			updateUserInfo() {

				this.userInfo.nickname = this.nickname || ''
				this.userInfo.avatar = this.avatarUrl || ''
				uni.setStorageSync('userInfo', this.userInfo);
				uni.setStorageSync('avatar', this.avatarUrl);
			},
			async getUserInfo() {
				let userinfo = await this.$rqt.userInfo();
				console.log(userinfo);
			},
			async save() {
				let data = {
					nickname: this.nickname || '',
					avatar: this.avatar || '',
				}

				// 保存用户数据
				let res = await this.$rqt.edituserInfo(data);
				if (res.code == 1) {
					this.updateUserInfo()
					uni.showToast({
						title: '修改成功',
						icon: "success"
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 600)

				} else {
					uni.showToast({
						title: '修改失败',
						icon: "error"
					})
				}

			},
			upload() {
				if (!this.isUpdateAvatar) {
					return;
				}
				let img = this.avatarUrl;
				if (img) {
					uni.showLoading({
						title: '上传中'
					})
					this.$rqt.uploadPhoto(img, (res) => {
						let data = JSON.parse(res.data);
						uni.hideLoading()
						console.log(data);
						this.uploadInfo = data;
						this.setupload();
					}, (err) => {
						uni.hideLoading()
						console.log(err);
					})
				}
			},
			setupload() {
				const {
					code,
					data,
				} = this.uploadInfo
				console.log(this.uploadInfo);
				if (code == 1) {
					console.log('9999', data);
					console.log('666', data.fullurl);
					this.avatar = data.fullurl || '';
				}
			},
			confirmLogout() {
				let that = this
				uni.showModal({
					title: '退出',
					content: '确定退出',
					success: function(res) {
						if (res.confirm) {
							that.logout()
						} else if (res.cancel) {

						}
					}
				});
			},
			async logout() {
				let that = this
				let res = await that.$rqt.logout();
				if (res.code == 1) {
					try {
						uni.clearStorageSync();
					} catch (e) {
						// error
					}
					uni.showToast({
						title: '退出成功',
						icon: "success"
					})
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}, 600)

				} else {
					uni.showToast({
						title: '退出失败',
						icon: "error"
					})
				}
			}

		},
		computed: {

		},
	}
</script>

<style lang="scss" scoped>
	.save {
		color: #fff;
		font-size: 30rpx;
		width: 90%;
		margin: 0 auto;
		height: 80rpx;
		line-height: 80rpx;
		font-weight: bold;
		background: #1890FF;
		box-shadow: 0px 0px 4px 0px rgba(204, 204, 204, 0.5);
		border-radius: 25px;
	}

	.logout {
		background: #9E9E9E;
	}

	.list {
		height: 50px;
		line-height: 50px;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.list>p {
		width: 100px;
		font-size: 18px;
		font-weight: 500;
	}

	.list>input {
		width: 80%;
		line-height: 50px;
		height: 50px;
		border-bottom: 1px solid #EEEEEE;
	}

	.box {
		overflow: hidden;
		padding-top: 20px;
		width: 90%;
		margin: 0 auto;
	}

	.headerimg {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
	}

	.nikename {
		text-align: right;
	}

	.avatar-wrapper {
		padding: 0;
		margin: 0;
		border-radius: 0px;
		background-color: white;
		line-height: initial;
	}

	button::after {
		display: none;
	}
</style>